
package com.idealighter.game.games.errentexaspoker.handler;

import com.idealighter.game.core.annotation.ResMsgHandler;
import com.idealighter.game.dictionary.domain.ErrentexaporkerRoomDomain;
import com.idealighter.game.dictionary.domain.RobotConfigDomain;
import com.idealighter.game.gamehall.dto.MemInfo;
import com.idealighter.game.games.errentexaspoker.ErrenTexaspokerPlayer;
import com.idealighter.game.games.errentexaspoker.message.ReqExchangeChipsMsg;
import com.idealighter.game.games.errentexaspoker.message.ReqExitTableMsg;
import com.idealighter.game.games.errentexaspoker.message.ReqReadyMsg;
import com.idealighter.game.login.message.ReqLogoutMsg;
import com.idealighter.game.message.core.ResMessage;
import com.idealighter.game.robot.common.PlayerPosition;
import com.idealighter.game.robot.context.ApplicationContext;
import com.idealighter.game.robot.core.Player;
import com.idealighter.game.robot.handler.ResMessageHandler;
import com.idealighter.game.robot.scheduler.PlayerScheduler;
import com.idealighter.utils.code.RandCodeUtil;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@ResMsgHandler(515203)
public class ResEnterTableHandler implements ResMessageHandler {

  private static final Logger LOG = LoggerFactory.getLogger(ResEnterTableHandler.class);


  @Override
  public void action(Player player, ResMessage message) {
    // ResEnterTableMsg msg = (ResEnterTableMsg) message;
    long enterTablePlayerId = player.playerId;
    if (enterTablePlayerId == player.playerInfo.getId()) {
      player.position = PlayerPosition.TABLE;
      ErrenTexaspokerPlayer texaspokerPlayer = (ErrenTexaspokerPlayer) player;
      LOG.info("[二人德州扑克]玩家[{}][{}]进入桌子成功", player.userName, player.playerId);
      // 随机延时3到5秒兑换筹码和准备
      player.schedule(() -> {
        ErrentexaporkerRoomDomain roomDomain = ((ErrenTexaspokerPlayer) player).roomDomain;
        // com.idealighter.game.gamehall.dto.SeatInfo seat =
        // gameStrategy.seat(player.playerId);
        MemInfo memInfo = texaspokerPlayer.member(player.playerId);

        if (player.playerInfo.getGold() <= 0) {
          if (memInfo.getChips() < roomDomain.getMinOne() * 8 + roomDomain.getAfee()) {
            player.sendMsg(new ReqLogoutMsg());
            player.delayLogout();
          }
        } else if (memInfo.getChips() < roomDomain.getMinOne() * 8 + roomDomain.getAfee()) {
          exchangeChips(player);
          player.sendMsg(new ReqReadyMsg());
        }
      }, RandCodeUtil.random(3, 5), TimeUnit.SECONDS);
    } else {
      RobotConfigDomain robotCfg = player.configDomain;
      boolean gameWithPlayer = robotCfg.getGameWithPlayer() > 0;
      PlayerScheduler robotPlayerMgr = ApplicationContext.getBean(PlayerScheduler.class);

      // 机器人占桌以后，如果不能陪玩家对局，玩家进入桌子后，机器人需要离开桌子
      if (!robotPlayerMgr.isRobot(enterTablePlayerId) && !gameWithPlayer) {
        player.sendMsg(new ReqExitTableMsg());
      }
    }
  }

  /**
   * 进入桌子后兑换全部金币为筹码.
   * 
   * @param player .
   */
  private void exchangeChips(Player player) {
    ReqExchangeChipsMsg exchangeChipsMsg = new ReqExchangeChipsMsg();
    exchangeChipsMsg.setGold((int) player.playerInfo.getGold());
    player.sendMsg(exchangeChipsMsg);
  }
}
